package com.tianma315.admin.invocie.rest;

import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.ResponseUtil;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.invocie.domain.dto.InvoiceDto;
import com.tianma315.admin.invocie.domain.vo.InvoiceVo;
import com.tianma315.admin.invocie.service.InvoiceService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
//======`-.____`-.___\_____/___.-`____.-'======
//                   `=---='
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//         佛祖保佑       永无BUG

/**
 * Description
 * <p>
 * Created by zcm on 2019/5/23.
 */
@RestController
@RequestMapping("/invoice")
public class InvoiceRestController extends BaseController {

    @Autowired
    private InvoiceService invoiceService;

    @GetMapping("/list")
    Result<Page<InvoiceVo>> list(Integer pageNumber, Integer pageSize,@RequestParam(required = false) String searchKey) {
        Page<InvoiceVo> page = invoiceService.getPage(pageNumber, pageSize, searchKey);
        if (page != null)
            return Result.ok(page);

        return Result.fail();
    }


    @PostMapping("/add")
    @RequiresPermissions("agent:add")
    Result add(@Validated @RequestBody InvoiceDto invoice) {
        invoice.setCreateBy(getUserId());
        if (invoiceService.save(invoice)) {
            return Result.ok();
        }
        return Result.fail();
    }

    @PostMapping("/edit")
    @RequiresPermissions("agent:edit")
    Result edit(@Validated @RequestBody InvoiceDto invoice) {
        if (invoiceService.updateInvoiceById(invoice)) {
            return Result.ok();
        }
        return Result.fail();
    }

    @PostMapping("/remove")
    @RequiresPermissions("invoice:remove")
    Result delete(Long id) {
        if (invoiceService.deleteInvoiceById(id)) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 删除
     */
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("invoice:remove")
    public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
        for (int i = 0; i < ids.length; i++) {
            invoiceService.deleteInvoiceById(ids[i]);
        }
        return Result.ok();
    }


    /**
     * 导出模板
     */
    @GetMapping( "/exportInvoice")
    public void importInvoice(HttpServletResponse response){
        String fileName = "出库订单导入模板.xls";
        try {
            response = ResponseUtil.getResponse(response,fileName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HSSFWorkbook wb =invoiceService.exportInvoice();
        try(OutputStream os = response.getOutputStream()) {
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    /**
     * 导入excel
     */
    @ResponseBody
    @PostMapping( "/importInvoice")
    public Result importInvoice(MultipartFile file){
        if (!file.isEmpty()){
            invoiceService.importInvoice(file,getUserId());
        }
        return Result.ok();
    }


}
